package com.runen.infinitemovement.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.MyReflectionAdapter;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.ChallengeAPI;
import com.runen.infinitemovement.model.entity.AchievementEntity;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyAchievementsActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.rv_my_reflection)
    RecyclerView rvMyReflection;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.top_title)
    TextView topTitle;
    private MyReflectionAdapter myReflectionAdapter;
    private String targetId;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_achievements;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        topTitle.setText("我的成就");
        topBtnLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        targetId = getIntent().getStringExtra("targetId");
        srl.setOnRefreshListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        rvMyReflection.setLayoutManager(layoutManager);
        myReflectionAdapter = new MyReflectionAdapter(this);
        rvMyReflection.setAdapter(myReflectionAdapter);
        initData();
    }

    private void initData() {
        srl.setRefreshing(true);
        String str = "?targetId=%s";
        new ChallengeAPI().getAchievement(String.format(str, targetId), new BaseResultCallback<AchievementEntity>() {
            @Override
            public void onResponse(AchievementEntity response) {
                srl.setRefreshing(false);
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        myReflectionAdapter.setNewData(response.data);
                    }
                } else {
                    ToastUtils.showShort(response.msg);
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        initData();
    }
}